import { Component, OnInit } from '@angular/core';
import { NzModalService, NzMessageService } from 'ng-zorro-antd';
import { HttpService } from '@core/services/http.service';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';


@Component({
  selector: 'app-announcement',
  templateUrl: './announcement.component.html',
    styles  : [ `
    :host ::ng-deep .vertical-center-modal {
      display: flex;
      align-items: center;
      justify-content: center;
    }
    :host ::ng-deep .vertical-center-modal .ant-modal {
        top: 0;
    }
    ` ]
})
export class AnnouncementComponent implements OnInit {
    validateForm: FormGroup;
    data = [];
    popTitle;
    Aname;
    Adetail;
    AId;
    loading = false;
    modalVisible3 = false;
    _allChecked = false;
    _indeterminate = false;
    saveState = '';
    searchValue = '';
    _loading = false;

    ngOnInit() {
        this.validateForm = this.fb.group({
            A_Title         : [ null, [ Validators.required ] ],
            A_Context         : [ [], [ Validators.required ] ]
        });
        this.getData();
    }

    getFormControl(name) {
        return this.validateForm.controls[ name ];
    }

    getData() {
        let param = {Condition: this.searchValue};
        this.http.noticeList(param).subscribe(res => {
           this._loading = false;
           for (let i in res.list) {
               if (res.list[i].Content.length > 30) {
                   res.list[i].content = res.list[i].Content.slice(0, 30) + '…';
               } else {
                   res.list[i].content = res.list[i].Content;
               }
           }
           this.data = res.list;
        });
    }
    save() {
        this.loading = true;
        if (this.saveState === 'add') {
            if (this.validateForm.valid) {
                let param = {title: this.Aname, content: this.Adetail};
                this.http.noticeAdd(param).subscribe(res => {
                    this.modalVisible3 = false;
                    this.loading = false;
                    if (res.code == 0) {
                        this.createMessage('success', res.msg);
                        this.getData();
                    }else {
                        this.createMessage('error', res.msg);
                    }
                });
            } else {
                this.loading = false;
                this.createMessage('error', '请输入完整内容后再保存！');
            }

        }else {
            if (this.validateForm.valid) {
                let param = {title: this.Aname, content: this.Adetail, id: this.AId.toString()};
                this.http.noticeUpdate(param).subscribe(res => {
                    this.modalVisible3 = false;
                    this.loading = false;
                    if (res.code == 0) {
                        this.createMessage('success', res.msg);
                        this.getData();
                    }else {
                        this.createMessage('error', res.msg);
                    }
                });
            } else {
                this.loading = false;
                this.createMessage('error', '请输入完整内容后再保存！');
            }
        }

    }

    modal (state, data) {
        if (state === 'add') {
            this.Aname = '';
            this.Adetail = '';
            this.modalVisible3 = true;
            this.popTitle = '新建公告';
            this.saveState = 'add';
        }else if (state === 'update') {
            this.Aname = data.Title;
            this.Adetail = data.Content;
            this.AId = data.Id;
            this.popTitle = '修改公告';
            this.modalVisible3 = true;
            this.saveState = 'update';
        }

    }

    search () {
        this.getData();
    }

    delnotice (data) {
        let param = { id: data.Id.toString()};
        this.http.noticeDel(param).subscribe(res => {
            if (res.code == 0) {
                this.createMessage('success', res.msg);
                this.getData();
                param = null;
            }else {
                this.createMessage('error', res.msg);
                param = null;
            }
        });
    }

    showConfirm = (data) => {
        let that = this;
        this.confirmServ.confirm({
            title  : '您是否确认要删除这项内容',
            okText : '确定',
            cancelText: '取消',
            onOk() {
               that.delnotice(data);
            },
            onCancel() {
            }
        });
    }

    info(contentTpl, data) {
        this.Aname = data.Title;
        this.Adetail = data.Content;
        this.confirmServ.info({
            title: '查看详情',
            okText: '确定',
            content: contentTpl
        });
    }

    createMessage = (type, text) => {
        this._message.create(type, `${text}`);
    }

    constructor(
        private confirmServ: NzModalService,
        private http: HttpService,
        private _message: NzMessageService,
        private fb: FormBuilder
    ) {}
}
